<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>使用SOCK_DGRAM类型的socket实现的ping程序 - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="SOCK_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UDP通信的socket实现ping的实例，本文将提供完整的源程序，本文的程序在 Ubuntu 20.04 下测试通过，gcc 版本号 9.4.0；阅读本文需要熟悉socket编程，对初学者而言，本文有一定的难度。
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/network/0020-implement-ping-program-with-sock-dgram/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="使用SOCK_DGRAM类型的socket实现的ping程序" />
<meta property="og:description" content="SOCK_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UDP通信的socket实现ping的实例，本文将提供完整的源程序，本文的程序在 Ubuntu 20.04 下测试通过，gcc 版本号 9.4.0；阅读本文需要熟悉socket编程，对初学者而言，本文有一定的难度。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/network/0020-implement-ping-program-with-sock-dgram/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2023-03-16T16:43:29+08:00" />
<meta property="article:modified_time" content="2023-03-16T16:43:29+08:00" />

<meta itemprop="name" content="使用SOCK_DGRAM类型的socket实现的ping程序">
<meta itemprop="description" content="SOCK_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UDP通信的socket实现ping的实例，本文将提供完整的源程序，本文的程序在 Ubuntu 20.04 下测试通过，gcc 版本号 9.4.0；阅读本文需要熟悉socket编程，对初学者而言，本文有一定的难度。"><meta itemprop="datePublished" content="2023-03-16T16:43:29+08:00" />
<meta itemprop="dateModified" content="2023-03-16T16:43:29+08:00" />
<meta itemprop="wordCount" content="7931">
<meta itemprop="keywords" content="Linux,ICMP,网络编程,SOCK_DGRAM,ping,internet checksum," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="使用SOCK_DGRAM类型的socket实现的ping程序"/>
<meta name="twitter:description" content="SOCK_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UDP通信的socket实现ping的实例，本文将提供完整的源程序，本文的程序在 Ubuntu 20.04 下测试通过，gcc 版本号 9.4.0；阅读本文需要熟悉socket编程，对初学者而言，本文有一定的难度。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">使用SOCK_DGRAM类型的socket实现的ping程序</h1>

      <div class="post-meta">
        <span class="post-time"> 2023-03-16 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            <a href="/categories/c-language/"> C Language </a>
            <a href="/categories/network/"> Network </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-前言">1. 前言</a></li>
        <li><a href="#2-icmp协议">2. ICMP协议</a></li>
        <li><a href="#2-ping程序的工作机制">2. ping程序的工作机制</a></li>
        <li><a href="#3-ping的简单实现">3. ping的简单实现</a></li>
        <li><a href="#4-后记">4. 后记</a></li>
        <li><a href="#欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>SOCK_DGRAM类型的socket常用于UDP通信，本文将尝试把这种socket用于ICMP协议，并完成一个简单的ping程序。使用ping去测试某个主机是否可用可能是一件很平常的事，尽管ping非常普通，但是编写一个实现ping功能的程序却并不是那么简单，因为ping使用的ICMP协议并不是一个应用层协议，在网上看到的实现ping的例子大多使用raw socket去实现，不仅增加了解析IP报头的麻烦，而且还需要有root权限才能运行，本文简要介绍ICMP协议，并给出一个使用普通的常用于UDP通信的socket实现ping的实例，本文将提供完整的源程序，本文的程序在 Ubuntu 20.04 下测试通过，gcc 版本号 9.4.0；阅读本文需要熟悉socket编程，对初学者而言，本文有一定的难度。</p>
<h2 id="1-前言">1. 前言</h2>
<ul>
<li>
<p>ICMP协议和UDP一样，都是面向无连接的；</p>
</li>
<li>
<p>发送一个ICMP数据包和发送一个UDP数据包非常类似，对UDP而言是构建一个UDP报头然后和数据一起发出去，对ICMP而言就是构建一个ICMP报头然后和数据一起发出去；</p>
</li>
<li>
<p>创建一个socket时，常用的socket类型有三种：SOCK_STREAM、SOCK_DGRAM和SOCK_RAW，SOCK_STREAM常用于TCP通信，SOCK_DGRAM常用于UDP通信，SOCK_RAW用于接收和发送原始数据包；</p>
</li>
<li>
<p>其实socket的种类也不止这三种，这些socket类型定义在头文件&lt;bits/socket_type.h&gt;中，但除了常用的这三个外，其它的基本都还没有实现，大多是因为缺少标准的协议支持，还有的是已经淘汰的socket类型，比如SOCK_PACKET；</p>
</li>
<li>
<p>可以用下面的代码测试在你的操作系统下，是否支持某个的socket类型，以SOCK_RDM为例：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;sys/socket.h&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kt">int</span> <span class="n">sock_fd</span> <span class="o">=</span> <span class="n">socket</span><span class="p">(</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">SOCK_RDM</span><span class="p">,</span> <span class="mi">0</span> <span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="n">perror</span><span class="p">(</span><span class="s">&#34;socket: &#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="n">printf</span><span class="p">(</span><span class="s">&#34;sock_fd: %d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">sock_fd</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>运行结果截图</p>
<p><img src="https://whowin.gitee.io/images/180020/screenshot-for-testing-sock-rdm.png" alt="Screenshot for testing SOCK_RDM"></p>
</li>
<li>
<p>SOCK_STREAM这种socket类型显然不适合用在ping程序上，因为这种socket是面向连接的，使用之前要先建立连接，但是如果可以建立了连接就完全没有必要使用ping去测试目的主机是否可用了，SOCK_RAW过于复杂而且必须要有root权限才能运行，我们放弃不用，所以最终我们使用SOCK_DGRAM来尝试发送ICMP数据包以实现一个ping程序；</p>
</li>
</ul>
<h2 id="2-icmp协议">2. ICMP协议</h2>
<ul>
<li>
<p>经常编写网络程序的程序员应该都很熟悉IP协议，IP协议没有任何内在机制来发送错误和控制消息，也就是说如果网络通信出现问题，IP协议本身是无法得知原因的，所以需要ICMP协议来帮助IP协议来完成这件事；</p>
</li>
<li>
<p>ICMP(Internet Control Message Protocol)协议是互联网协议族中的一个支持协议，用于在IP协议中发送控制信息，报告网络通信中可能发生的问题；</p>
</li>
<li>
<p>通常认为ICMP有两个主要作用：</p>
<ol>
<li>
<p>报告错误</p>
<blockquote>
<p>当两个设备通过互联网连接时，ICMP会生成错误信息并将该信息发回到发送设备上，以防发送数据未到达其预期目的地；例如：一个数据包的长度大于某个路由器所能接收的最大长度，路由器将丢弃该数据包并将 ICMP 消息发送回数据的发送设备。</p>
</blockquote>
</li>
<li>
<p>执行网络诊断</p>
<blockquote>
<p>常用的终端实用程序 <code>traceroute</code> 和 <code>ping</code> 都是使用<strong>ICMP</strong>协议运行；<code>traceroute</code> 实用程序用于显示两个互联网设备之间的路由路径；路由路径是请求数据到达目的地之前必须经过的路由器的实际物理路径；一个路由器与另一个路由器之间的路径称为&quot;跃点&quot;，路由跟踪还会报告经过每个跃点所需的时间；这对于确定网络延迟来源是非常有用的。</p>
</blockquote>
</li>
</ol>
</li>
<li>
<p>ICMP协议常被归为网络层协议，但是ICMP报文是被包装在一个IP报文中的，把ICMP、IP同归为网络层似乎也不是那么合适；</p>
</li>
<li>
<p>但是ICMP与常用的传输层协议TCP和UDP也有明显的不同，因为它通常不会用于在两个或多个计算机系统之间传输数据，所以把ICMP称为传输层协议似乎也不大合适，所以，也有人说ICMP协议是IP协议的附属协议或者说ICMP是介于网络层和传输层之间的中间层协议；</p>
</li>
<li>
<p>ICMP是那一层的协议其实并不重要，重要的是我们可以理解和使用它；实际上在设计ICMP协议时并没有考虑OSI的网络模型，所以才造成了这种含混的现象，不过这并不影响它的使用；</p>
</li>
<li>
<p>和发送UDP报文相比，发送ICMP报文仅仅是两个协议的报头不一样而已，所以我们先了解ICMP报头结构，如果需要了解以太网报头、IP报头和UDP报头，可以参考文章：<a href="https://blog.csdn.net/whowin/article/details/128766145">《Linux下如何在数据链路层接收原始数据包》</a>；</p>
</li>
<li>
<p>ICMP报文的结构：</p>
<p><img src="https://whowin.gitee.io/images/180019/structure-of-icmp-packet.png" alt="Structure of ICMP packet"></p>
</li>
<li>
<p>ICMP报头，定义在头文件&lt;netinet/ip_icmp.h&gt;中</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">icmphdr</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kt">uint8_t</span> <span class="n">type</span><span class="p">;</span>        <span class="cm">/* message type */</span>
</span></span><span class="line"><span class="cl">  <span class="kt">uint8_t</span> <span class="n">code</span><span class="p">;</span>        <span class="cm">/* type sub-code */</span>
</span></span><span class="line"><span class="cl">  <span class="kt">uint16_t</span> <span class="n">checksum</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="k">union</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="kt">uint16_t</span> <span class="n">id</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">      <span class="kt">uint16_t</span> <span class="n">sequence</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span> <span class="n">echo</span><span class="p">;</span>               <span class="cm">/* echo datagram */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">uint32_t</span> <span class="n">gateway</span><span class="p">;</span>     <span class="cm">/* gateway address */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="kt">uint16_t</span> <span class="n">__glibc_reserved</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">      <span class="kt">uint16_t</span> <span class="n">mtu</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span> <span class="n">frag</span><span class="p">;</span>               <span class="cm">/* path mtu discovery */</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span> <span class="n">un</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>type：ICMP报文类型，定义在头文件&lt;netinet/ip_icmp.h&gt;中：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define ICMP_ECHOREPLY      0     </span><span class="cm">/* Echo Reply             */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_DEST_UNREACH   3     </span><span class="cm">/* Destination Unreachable*/</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_SOURCE_QUENCH  4     </span><span class="cm">/* Source Quench          */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_REDIRECT       5     </span><span class="cm">/* Redirect (change route)*/</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_ECHO           8     </span><span class="cm">/* Echo Request           */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_TIME_EXCEEDED  11    </span><span class="cm">/* Time Exceeded          */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_PARAMETERPROB  12    </span><span class="cm">/* Parameter Problem      */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_TIMESTAMP      13    </span><span class="cm">/* Timestamp Request      */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_TIMESTAMPREPLY 14    </span><span class="cm">/* Timestamp Reply        */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_INFO_REQUEST   15    </span><span class="cm">/* Information Request    */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_INFO_REPLY     16    </span><span class="cm">/* Information Reply      */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_ADDRESS        17    </span><span class="cm">/* Address Mask Request   */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define ICMP_ADDRESSREPLY   18    </span><span class="cm">/* Address Mask Reply     */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define NR_ICMP_TYPES       18
</span></span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>本文只关心ICMP_ECHO和ICMP_ECHOREPLY两种报文类型，ping的实现实际上就是发送ICMP_ECHO报文，然后等待目的主机响应ICMP_ECHOREPLY报文，当然出现错误时可能会收到其他类型的报文，但本文的实例中将不予处理；</li>
<li>code：子类型编码，在我们要用到的这两种类型的ICMP报文中，code都是为0的，也就是用不上这个字段，但有些报文类型是要用到code的，比如ICMP_DEST_UNREACH类型的报文，表示目标不可达，这个报文中的code字段的值将说明目标不可达的原因；</li>
<li>checksum：ICMP报文的检查和，计算这个检查和时要包括ICMP报头和ICMP数据，其计算方法与internet checksum的计算方法一致，可以参考文章<a href="https://blog.csdn.net/whowin/article/details/128766194">《如何计算UDP头的checksum》</a>和<a href="https://blog.csdn.net/whowin/article/details/128846658">《如何计算IP报头的checksum》</a></li>
<li>由于我们仅关心ICMP_ECHO和ICMP_ECHOREPLY这两类报文，所以在ICMP报头的union中，我们使用echo这个结构：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">uint16_t</span> <span class="n">id</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">uint16_t</span> <span class="n">sequence</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span> <span class="n">echo</span><span class="p">;</span>               <span class="cm">/* echo datagram */</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>id：一个唯一的ID，用于标识收到的报文是由当前进程发出，一般可以使用进程ID作为这个标识，也可以是其他的任意唯一标识；</li>
<li>sequence：icmp数据包的序列号；也就是一个编号，通常使用一个递增的序号，发的第一个报文为1，第二个报文为2，&hellip;&hellip;</li>
</ul>
</li>
</ul>
</li>
<li>
<p>ICMP报头结构就是这么几个字段，但是比起UDP的报头还是要复杂一些，比如UDP报头中虽然也有checksum字段，但这个字段在IPv4中不是强制的，可以不填，但是ICMP报头中的checksum字段是必须要计算的，计算的不对报文将无法送达。</p>
</li>
</ul>
<h2 id="2-ping程序的工作机制">2. ping程序的工作机制</h2>
<ul>
<li><code>ping</code> 程序的工作原理很像声纳回声定位，执行<code>ping</code>程序的主机向目标主机发送一个 ICMP_ECHO 请求数据包，然后目标主机返回一个 ICMP_ECHOREPLY 数据包；</li>
<li><code>ping</code> 程序是一个基本的Internet工具，可以验证一个 IP 或者主机名称所指向的主机是否存在并可以响应请求；</li>
<li><code>ping</code> 程序通过打开一个 socket 来发送 ICMP_ECHO 请求数据包，然后等待目标主机的响应，如果数据包顺利到达目标主机，而且主机也为可用状态，主机的<strong>内核</strong>将返回一个 ICMP_ECHOREPLY 数据包，如果出现错误，主机或者其他相关的网络设备将返回一个 ICMP 的错误信息数据包；</li>
<li>要注意的是 ICMP_ECHOREPLY 是由内核发出的，与任何应用层的程序无关；</li>
<li>IP报头中有一个TTL(Time To Live)值，该值决定了路由器的最大跳数(就是报文经过的路由器最大数量，一般定为64)，当经过的路由器超过这个数量时，路由器将丢弃该数据包；</li>
<li>如果数据包没有到达，那么发送方将收到一个错误信息，错误信息有以下类型：
<ol>
<li>传输过程中 TTL 过期</li>
<li>目标主机不可达</li>
<li>请求超时(即没有收到回复)</li>
<li>其它原因</li>
</ol>
</li>
<li>由此可见，一个ping程序可能会遇到以下类型的ICMP数据包(参考ICMP报头结构)：
<ol>
<li>ICMP_ECHO请求 - type=ICMP_ECHO(8)，code=0</li>
<li>ICMP_ECHOREPLY应答 - type=ICMP_ECHOREPLY(0)，code=0</li>
<li>TTL 过期 - type=ICMP_TIME_EXCEEDED(11)，code=0</li>
<li>目标主机不可达 - type=ICMP_DEST_UNREACH(3)，code=0-5</li>
<li>没有收到回复 - 接收超时</li>
</ol>
</li>
<li>当我们发出一个 ICMP ECHO 请求后，我们收到的回应并不一定来自目标主机，比如我们收到了一个type=3的ICMP数据包，也就是目标不可达的错误，当code=2时表示主机不可达，此时主机是不可能发出消息的，这个ICMP数据包一般会由目标主机的 gateway 发出；</li>
<li>ICMP协议是一种面向无连接的协议，发送数据包之前无需像TCP一样先建立连接；</li>
</ul>
<h2 id="3-ping的简单实现">3. ping的简单实现</h2>
<ul>
<li>
<p>本文并不寻求实现 Linux 下目前存在的 ping 的所有功能，那样过于复杂，本文仅实现ping的基本功能；</p>
</li>
<li>
<p>功能列表</p>
<ol>
<li>接收 IP 或者主机名作为输入，并可以自动识别</li>
<li>对主机名执行 DNS lookup，将其转换为 IP</li>
<li>ctrl+c可以随时终止程序，并在终止时显示数据统计报告</li>
<li>按照一定的时间间隔向目标主机发送ICMP_ECHO报文，并等待目标主机回应ICMP_ECHOREPLY报文</li>
<li>接收超时则认为丢失报文</li>
<li>如果收到的报文不是ICMP_ECHOREPLY，则认为报文丢失</li>
</ol>
</li>
<li>
<p>实现步骤</p>
<ol>
<li>
<p><strong>检查命令行输入的参数</strong></p>
<blockquote>
<p>判断命令行的输入是IP地址还是主机名，如果是主机名，要对主机名执行DNS lookup将其转换为IP地址</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">hostent</span> <span class="o">*</span><span class="n">host</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">in_addr</span> <span class="n">ipaddr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">((</span><span class="n">host</span> <span class="o">=</span> <span class="n">gethostbyname</span><span class="p">(</span><span class="n">hostname</span><span class="p">))</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// fail to DNS lookup
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">herror</span><span class="p">(</span><span class="s">&#34;gethostbyname()&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="n">ipaddr</span><span class="p">.</span><span class="n">s_addr</span> <span class="o">=</span> <span class="o">*</span><span class="p">(</span><span class="n">in_addr_t</span> <span class="o">*</span><span class="p">)</span><span class="n">host</span><span class="o">-&gt;</span><span class="n">h_addr_list</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>获取ICMP协议的编号</strong></p>
<blockquote>
<p>执行getprotobyname()获取ICMP的协议号是为了下面建立socket时有一个正确的协议号，其实不用这么麻烦，直接用宏IPPROTO_ICMP也是完全可以的；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">protoent</span> <span class="o">*</span><span class="n">protocol</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">protocol</span> <span class="o">=</span> <span class="n">getprotobyname</span><span class="p">(</span><span class="s">&#34;icmp&#34;</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>建立socket</strong></p>
<blockquote>
<p>如果上面没有执行getprotobyname()获取协议号，这里的协议号直接用IPPROTO_ICMP没有问题；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">sockfd</span> <span class="o">=</span> <span class="n">socket</span><span class="p">(</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">SOCK_DGRAM</span><span class="p">,</span> <span class="n">protocol</span><span class="o">-&gt;</span><span class="n">p_proto</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>设置接收超时，以避免接收时阻塞</strong></p>
<blockquote>
<p>设置接收超时是为了防止接收ICMP_ECHOREPLY时出现阻塞导致程序无法继续运行；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define RECV_TIMEOUT    5
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">timeval</span> <span class="n">timeout</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">timeout</span><span class="p">.</span><span class="n">tv_sec</span>  <span class="o">=</span> <span class="n">RECV_TIMEOUT</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">timeout</span><span class="p">.</span><span class="n">tv_usec</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">setsockopt</span><span class="p">(</span><span class="n">sockfd</span><span class="p">,</span> <span class="n">SOL_SOCKET</span><span class="p">,</span> <span class="n">SO_RCVTIMEO</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">timeout</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">timeout</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>设置IP层的TTL(Time To Live)</strong></p>
<blockquote>
<p>TTL其实就是报文最多经过多少路由器；TTL是个非常重要的参数，如果没有TTL，数据包有可能在互联网上无限循环，数据包每经过一个路由器时，TTL都会减1，当TTL为0时，路由器将丢弃该数据包；系统中会有一个默认的TTL值，一般这个默认值为64，查看默认的TTL值可以用命令 <code>cat /proc/sys/net/ipv4/ip_default_ttl</code> 或者 <code>sudo sysctl -a|grep ip_default_ttl</code>，TTL最大可以为255，所以实际上不设置TTL也不会有什么问题；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">ttl_val</span> <span class="o">=</span> <span class="mi">64</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">setsockopt</span><span class="p">(</span><span class="n">sockfd</span><span class="p">,</span> <span class="n">SOL_IP</span><span class="p">,</span> <span class="n">IP_TTL</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ttl_val</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">ttl_val</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>接管信号量SIGINT的处理程序</strong></p>
<blockquote>
<p>当按下ctrl+c时，将产生信号SIGINT，接管该信号意味着接管ctrl+c的处理程序；ping程序只能使用ctrl+c退出，所以接管该信号是必要的</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="n">signal</span><span class="p">(</span><span class="n">SIGINT</span><span class="p">,</span> <span class="n">sigint</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>记录开始时间</strong></p>
<blockquote>
<p>记录开始时间是为了退出时打印统计数据，统计数据中有一项是总的耗时时间，需要用到这个开始时间；所有时间均以时间戳的形式记录，计时的精度要达到0.00ms以上；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">timeval</span> <span class="n">start_time</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">gettimeofday</span><span class="p">(</span><span class="o">&amp;</span><span class="n">start_time</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>构建一个ICMP报文</strong></p>
<ul>
<li>
<p>一个ICMP报文就是一个ICMP报头+数据，通常一个ping数据包的数据部分为56个字节，ICMP报头为8个字节，一起为64个字节，加上IP报头20个字节，一共为84个字节，但是，IPv4报文的最大长度可以达到65535字节，所以理论上ICMP报文可以很长，不一定非要64个字节，按照ICMP协议规定，ICMP_ECHOREPLY报文会把ICMP_ECHO请求报文中的数据全部返回回来；</p>
</li>
<li>
<p>一个ICMP报文最小长度是多少呢？IEEE 802.3标准中定义了一个以太网帧最小为64字节，这里面包含了以太网报头的14字节和帧结尾的4字节的CRC，这些占了18个字节，IP报头占用20字节，剩下留给ICMP报文的为：64 - 18 -20 = 26字节，ICMP报头占8字节，所以ICMP报文数据为18字节，其实ICMP报文长度还可以小，但没有任何意义，因为数据帧还是要填充到64字节发出去；</p>
</li>
<li>
<p>关于ICMP报头也没什么好说的，type=ICMP_ECHO，返回来的ICMP数据包中type=ICMP_ECHOREPLY；code=0；checksum在计算前一定要先填0，这是计算checksum要求的；sequence一般是一个序号，从0或者1开始都没有关系，每发出一个数据包，sequence+1就好了；id可以填任意唯一标识，通常使用当前进程ID；</p>
</li>
<li>
<p>ICMP报文的数据部分，我们首先填了一个发送时的时间戳，ICMP_ECHO报文的数据部分会在ICMP_ECHOREPLY报文中完全返回来，所以这个时间戳会出现在收到的ICMP_ECHOREPLY报文中，我们会在收到ICMP_ECHOREPLY报文时利用这个时间戳计算icmp报文的往返时间；后面的数据我们填充上了字符'0&rsquo;，完全可以什么都不填，让后面的数据为随机数据；</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define ICMP_DATA_SIZE      (64 - sizeof(struct icmphdr));
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">send_buf</span><span class="p">[</span><span class="mi">512</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">pack_size</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">icmphdr</span><span class="p">)</span> <span class="o">+</span> <span class="n">ICMP_DATA_SIZE</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">icmphdr</span> <span class="o">*</span><span class="n">icmp_hdr</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">icmphdr</span> <span class="o">*</span><span class="p">)</span><span class="n">send_buf</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">timeval</span> <span class="o">*</span><span class="n">tval</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">timevar</span> <span class="o">*</span><span class="p">)(</span><span class="n">send_buf</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">icmphdr</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="kt">char</span> <span class="o">*</span><span class="n">icmp_data</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">send_buf</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">icmphdr</span><span class="p">)</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">timeval</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">icmp_hdr</span><span class="o">-&gt;</span><span class="n">type</span> <span class="o">=</span> <span class="n">ICMP_ECHO</span><span class="p">;</span>              <span class="c1">// ICMP_ECHO packet
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">icmp_hdr</span><span class="o">-&gt;</span><span class="n">code</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>                      <span class="c1">// code=0
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">icmp_hdr</span><span class="o">-&gt;</span><span class="n">checksum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>                  <span class="c1">// checksum will be calculated later
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">icmp_hdr</span><span class="o">-&gt;</span><span class="n">un</span><span class="p">.</span><span class="n">echo</span><span class="p">.</span><span class="n">sequence</span> <span class="o">=</span> <span class="n">pack_no</span><span class="p">;</span>    <span class="c1">// serial no
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">icmp_hdr</span><span class="o">-&gt;</span><span class="n">un</span><span class="p">.</span><span class="n">echo</span><span class="p">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">getpid</span><span class="p">();</span>         <span class="c1">// process id
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl"><span class="n">gettimeofday</span><span class="p">(</span><span class="n">tval</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>                <span class="c1">// fill a sending timestamp into data
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">pack_size</span> <span class="o">-</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">timeval</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">memset</span><span class="p">(</span><span class="n">icmp_data</span><span class="p">,</span> <span class="sc">&#39;0&#39;</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>               <span class="c1">// fill &#39;0&#39; into rest place of send_buf
</span></span></span><span class="line"><span class="cl"><span class="c1">// checksum
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">icmp_hdr</span><span class="o">-&gt;</span><span class="n">checksum</span> <span class="o">=</span> <span class="n">checksum</span><span class="p">((</span><span class="kt">uint16_t</span> <span class="o">*</span><span class="p">)</span><span class="n">ping_p</span><span class="p">,</span> <span class="n">pack_size</span><span class="p">);</span> 
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
</li>
<li>
<p><strong>计算ICMP报文的checksum</strong></p>
<blockquote>
<p>构建ICMP报文的最后一步就是计算checksum，这里仅给出程序，需要了解计算方法的可以参考文章<a href="https://blog.csdn.net/whowin/article/details/128766194">《如何计算UDP头的checksum》</a>和<a href="https://blog.csdn.net/whowin/article/details/128846658">《如何计算IP报头的checksum》</a></p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">uint16_t</span> <span class="nf">checksum</span><span class="p">(</span><span class="kt">uint16_t</span> <span class="o">*</span><span class="n">addr</span><span class="p">,</span> <span class="kt">int</span> <span class="n">len</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">register</span> <span class="kt">long</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">uint16_t</span> <span class="o">*</span><span class="n">w</span> <span class="o">=</span> <span class="n">addr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">uint16_t</span> <span class="n">check_sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">nleft</span> <span class="o">=</span> <span class="n">len</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">while</span> <span class="p">(</span><span class="n">nleft</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">sum</span> <span class="o">+=</span> <span class="o">*</span><span class="n">w</span><span class="o">++</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">nleft</span> <span class="o">-=</span> <span class="mi">2</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// Add left-over byte, if any
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">if</span> <span class="p">(</span><span class="n">nleft</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">check_sum</span> <span class="o">=</span> <span class="o">*</span><span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="n">w</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">sum</span> <span class="o">+=</span> <span class="n">check_sum</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// Add carries
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">while</span> <span class="p">(</span><span class="n">sum</span> <span class="o">&gt;&gt;</span> <span class="mi">16</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="n">sum</span> <span class="o">=</span> <span class="p">(</span><span class="n">sum</span> <span class="o">&amp;</span> <span class="mh">0xffff</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">sum</span> <span class="o">&gt;&gt;</span> <span class="mi">16</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">check_sum</span> <span class="o">=</span> <span class="o">~</span><span class="p">(</span><span class="kt">uint16_t</span><span class="p">)</span><span class="n">sum</span><span class="p">;</span>     <span class="c1">// one&#39;s complement
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">return</span> <span class="n">check_sum</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>发送ICMP_ECHO报文</strong></p>
<blockquote>
<p>像发送一个UDP报文那样，我们使用sendto()发送ICMP报文，ipaddr是在一开始执行DNS lookup时得到的目标IP地址，端口号port在这里设置为0，但实际上填上多少都没关系，比如1025，接收端会完全忽略掉这个值；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">sockaddr_in</span> <span class="n">dest_addr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">d_addr</span><span class="p">.</span><span class="n">sin_family</span> <span class="o">=</span> <span class="n">AF_INET</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">d_addr</span><span class="p">.</span><span class="n">sin_port</span> <span class="o">=</span> <span class="n">htons</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">dest_addr</span><span class="p">.</span><span class="n">sin_addr</span><span class="p">.</span><span class="n">s_addr</span> <span class="o">=</span> <span class="n">ipaddr</span><span class="p">.</span><span class="n">s_addr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">sendto</span><span class="p">(</span><span class="n">sockfd</span><span class="p">,</span> <span class="n">send_buf</span><span class="p">,</span> <span class="n">packet_size</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">dest_addr</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_in</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>接收返回的ICMP_ECHOREPLY报文</strong></p>
<blockquote>
<p>像接收一个UDP报文一样，我们使用recvfrom()接收报文，在第4步时已经设置了接收超时，所以这里的recvfrom()不会阻塞很久，如果产生超时，我们就认为数据包丢失；我们这里设置的接收缓冲区大小是固定的，如果你要用这个程序发送很大的ICMP_ECHO包时，小心返回的ICMP_ECHOREPLY可能无法完整接收；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define RECV_BUF_SIZE       1024
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="kt">char</span> <span class="n">recv_buf</span><span class="p">[</span><span class="n">RECV_BUF_SIZE</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">sockaddr_in</span> <span class="n">from</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">from_len</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_in</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">recvfrom</span><span class="p">(</span><span class="n">sockfd</span><span class="p">,</span> <span class="n">recv_buf</span><span class="p">,</span> <span class="n">RECV_BUF_SIZE</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">from</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">from_len</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>检查收到的报文的checksum</strong></p>
<blockquote>
<p>在计算收到的报文的checksum之前，要先检查这个报文是不是一个ICMP_ECHOREPLY报文，本例不处理其它ICMP报文；其次要检查ICMP报头中的ID是否为当前进程的ID(在发送ICMP_ECHO报文时设置的)，第三要通过ICMP报头中的sequence判断该报文是否为重复报文(就是已经收到过相同sequence的报文)；</p>
</blockquote>
<blockquote>
<p>用前面的checksum()运算一个带有checksum字段的ICMP报文，其结果应该为0，否则就是报文有问题。</p>
</blockquote>
</li>
<li>
<p><strong>计算报文的往返时间，填写统计数据</strong></p>
<blockquote>
<p>在发送ICMP_ECHO报文时，我们在数据包的数据部分填写了一个发送时的时间戳，为了统计数据需要，我们需要记录下面几个数据，每组icmp报文的往返时间之和<strong>sum_time</strong>、每组往返时间平方之和<strong>qsum_time</strong>、最小往返时间<strong>min_time</strong>和最大往返时间<strong>max_time</strong>，当然还要记录发出的icmp报文的数量<strong>nsend</strong>和收到的icmp报文的数量<strong>nreceived</strong>，至于这些数据的应用，我们在后面会提到；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">timeval</span> <span class="o">*</span><span class="n">send_time_p</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">timeval</span> <span class="o">*</span><span class="p">)(</span><span class="n">recv_buf</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">icmphdr</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">timeval</span> <span class="n">recv_time</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">gettimeofday</span><span class="p">(</span><span class="o">&amp;</span><span class="n">recv_time</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>             <span class="c1">// Receive time
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl"><span class="kt">float</span> <span class="n">interval</span> <span class="o">=</span> <span class="p">(</span><span class="n">recv_time</span><span class="p">.</span><span class="n">tv_sec</span> <span class="o">-</span> <span class="n">send_time_p</span><span class="o">-&gt;</span><span class="n">tv_sec</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1000.00</span> <span class="o">+</span> <span class="p">((</span><span class="n">recv_time</span><span class="p">.</span><span class="n">tv_usec</span> <span class="o">-</span> <span class="n">send_time_p</span><span class="o">-&gt;</span><span class="n">tv_usec</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.00</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">sum_time</span> <span class="o">+=</span> <span class="n">interval</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">qsum_time</span> <span class="o">+=</span> <span class="p">(</span><span class="n">interval</span> <span class="o">*</span> <span class="n">interval</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span><span class="n">interval</span> <span class="o">&lt;</span> <span class="n">min_time</span><span class="p">)</span> <span class="n">min_time</span> <span class="o">=</span> <span class="n">interval</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span><span class="n">interval</span> <span class="o">&gt;</span> <span class="n">max_time</span><span class="p">)</span> <span class="n">max_time</span> <span class="o">=</span> <span class="n">interval</span><span class="p">;</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>返回步骤8，发送下一个报文</strong></p>
<blockquote>
<p>当然要先打印出当前icmp报文的状况后再返回步骤8，开始发送下一个报文；</p>
</blockquote>
</li>
<li>
<p><strong>ctrl+c处理程序</strong></p>
<blockquote>
<p>前面我们拦截了ctrl+c的信号，这个信号的处理非常简单，只要使发送-接收icmp报文的循环结束即可，实际上就是在步骤14时不要再返回步骤8，而是直接打印统计结果然后退出程序；本例我们使用了一个公共变量ping_loop来控制循环，ping_loop=true时循环继续，否则循环停止；</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">sigint</span><span class="p">(</span><span class="kt">int</span> <span class="n">signum</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">ping_loop</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>打印统计数据</strong></p>
</li>
</ol>
<ul>
<li>
<p>我们先看一下Linux(Ubuntu)提供的ping程序的统计结果输出的截屏</p>
<p><img src="https://whowin.gitee.io/images/180020/screenshot-of-ping-statistics.png" alt="screetshot of ping statistics"></p>
</li>
<li>
<p>其统计数据有</p>
<ol>
<li>发送的ICMP_ECHO报文数量：nsend</li>
<li>接收到的ICMP_ECHOREPLY报文数量：nreceived</li>
<li>丢失的ICMP_ECHOREPLY报文数量：nsend - nreceived</li>
<li>最小往返时间(min)：min_time</li>
<li>最大往返时间(max)：max_time</li>
<li>平均往返时间(avg)：$\large {\sum_{1}^{n}rtt \over n} = {{sum_time} \over nreceived} $</li>
</ol>
<blockquote>
<p>rtt是Round Trip Time的意思，意即数据包的往返时间</p>
</blockquote>
<ol start="7">
<li>平均偏差(mdev)</li>
</ol>
<blockquote>
<p>mdev是Mean Deviation的意思，它表示这些ICMP的往返时间rtt偏离平均值的程度，一般认为这个值越大，网络的稳定性越差，这个值的计算公式为：</p>
</blockquote>
<p>$$
{\sqrt{{\sum{x_i^2} \over n} - ({\sum{x_i} \over n})^2}} = {\sqrt{{qsum_time \over nreceived} - {({sum_time \over nreceived})^2}}} = {\sqrt{{qsum_time \over nreceived} - avg^2}}
$$</p>
</li>
<li>
<p>下面是统计数据的主要代码</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">float</span> <span class="n">avg</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">mdev</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span><span class="n">nreceived</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">avg</span> <span class="o">=</span> <span class="p">(</span><span class="n">sum_time</span> <span class="o">*</span> <span class="mf">1.00</span><span class="p">)</span> <span class="o">/</span> <span class="n">nreceived</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">mdev</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(((</span><span class="n">qsum_time</span> <span class="o">*</span> <span class="mf">1.00</span><span class="p">)</span> <span class="o">/</span> <span class="n">nreceived</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">avg</span> <span class="o">*</span> <span class="n">avg</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
</li>
<li>
<p>完整源代码，文件名：<a href="https://gitee.com/whowin/whowin/blob/blog/sourcecodes/180020/ping-dgram.c">ping-dgram.c</a>(<strong>点击文件名下载源程序</strong>)</p>
</li>
<li>
<p>编译，因为在统计部分使用了数学函数，所以编译时要加上 <code>-lm</code> 选项，意即连接数学函数库</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">gcc -Wall ping-dgram.c -o ping-dgram -lm
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>运行：<code>./ping-dgram baidu.com</code></p>
</li>
<li>
<p>运行截图</p>
<p><img src="https://whowin.gitee.io/images/180020/screenshot-of-ping-dgram.png" alt="screenshot of ping-dgram"></p>
</li>
</ul>
<h2 id="4-后记">4. 后记</h2>
<ul>
<li>ping的输出中有一项是TTL，文中多次提到了这个值的意义，本例输出的这个值可能并不准确，这个值的准确值是放在IP报头中的，但本例使用的方法是读取不到IP报头的，所以无法取得准确的TTL值，这应该是使用SOCK_DGRAM类型的socket编写ping程序的一个小缺陷，在本例中我们使用了初始化socket时设置的TTL值进行了显示；</li>
<li>代码中对重复报文做了判断，其判断本身也并不是很准确，正常情况下是不应该收到重复的ICMP_ECHOREPLY报文的，但根据我的经验，除了在局域网中有重复IP的情况以外，多发生在网络中有多个并行路由器的情况下，有时是因为某台机器开启了某些有路由功能的进程，比如网络中有一台运行openWrt的机器，很可能上面就会运行有一些操作路由的进程，但是更详细的产生重复ICMP报文的情况并不十分清楚，因为这种情况并不多见，也不太容易捕捉到；</li>
<li>本例中仅仅处理了ICMP_ECHOREPLY报文，但实际上一个ICMP_ECHO报文发出后，只有在一切正常的情况下才返回ICMP_ECHOREPLY报文，如果出现错误，会返回其他类型的报文，比如：ICMP_DEST_UNREACH报文，表示报文没有到达目的地，code中将标识没有到达的原因，其实这类报文更有意义，如果读者有兴趣，可以在本例的基础上进行扩展，写出更完美的ping程序；</li>
<li>文中提到，一个ICMP报文可以很长，如果想用本例中的程序去测试比较长的ICMP报文，请注意本例的接收缓冲区的大小是一个固定值，请进行调整，避免缓冲区溢出或接收不到完整数据包的情况发生。</li>
</ul>
<h2 id="欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></h2>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
<!--gitee
[article01]:https://whowin.gitee.io/post/blog/network/0003-checksum-of-udp-header/
[article02]:https://whowin.gitee.io/post/blog/network/0004-checksum-of-ip-header/
[article03]:https://whowin.gitee.io/post/blog/network/0002-link-layer-programming/
-->
<!-- CSDN
[img01]:https://img-blog.csdnimg.cn/img_convert/255e010e19c0378b34781fc13af4afdb.png
[img02]:https://img-blog.csdnimg.cn/img_convert/a30e5f4a21a95a4cdf90faf2d78b1eda.png
[img03]:https://img-blog.csdnimg.cn/img_convert/26795655ca48d8d9b3db424265d25dc3.png
[img04]:https://img-blog.csdnimg.cn/img_convert/7a881c0cbe31c06fa67b2b706d8c5774.png
-->
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2023-03-16
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">Linux</a>
          <a href="/tags/icmp/">ICMP</a>
          <a href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
          <a href="/tags/sock_dgram/">SOCK_DGRAM</a>
          <a href="/tags/ping/">ping</a>
          <a href="/tags/internet-checksum/">internet checksum</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/network/0019-dns-client-in-c/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">用C语言实现的一个DNS客户端</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/network/0018-tun-example-for-setting-up-ip-tunnel/">
            <span class="next-text nav-default">使用tun虚拟网络接口建立IP隧道的实例</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/network/0020-implement-ping-program-with-sock-dgram/" class="leancloud_visitors" data-flag-title="使用SOCK_DGRAM类型的socket实现的ping程序">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
